package com.cjq.goods.controller;

import com.cjq.common.domain.Goods;
import com.cjq.common.domain.request.GoodsReq;
import com.cjq.common.domain.response.GoodsRes;
import com.cjq.common.result.PageResult;
import com.cjq.common.result.Result;
import com.cjq.goods.service.GoodsService;
import com.cjq.goods.util.OssUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OssUtil ossUtil;

    /**
     * 商品列表
     * @param goodsReq
     * @return
     */
    @PostMapping("/goodsList")
    public Result<PageResult<GoodsRes>> goodsList(@RequestBody GoodsReq goodsReq){
        PageHelper.startPage(goodsReq.getPageNum(),goodsReq.getPageSize());
        List<GoodsRes> goodsResList = goodsService.goodsList(goodsReq);
        PageInfo<GoodsRes> info = new PageInfo<>(goodsResList);
        Result<PageResult<GoodsRes>> result = PageResult.toResult(info.getTotal(), goodsResList);
        return result;
    }

    /**
     * 添加
     * @param goods
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Goods goods){
        return goodsService.insert(goods);
    }

    /**
     * 修改
     * @param goods
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Goods goods){
        return goodsService.update(goods);
    }

    /**
     * 逻辑删除
     * @param goodsId
     * @return
     */
    @PutMapping("/updateStatus/{goodsId}")
    public Result updateStatus(@PathVariable String goodsId){
        return goodsService.updateStatus(goodsId);
    }

    /**
     * 单删
     * @param goodsId
     * @return
     */
    @DeleteMapping("/delete/{goodsId}")
    public Result del(@PathVariable String goodsId){
        return goodsService.del(goodsId);
    }

    /**
     * 批删
     * @param goodsIds
     * @return
     */
    @DeleteMapping("/batchDel")
    public Result batchDel(@RequestBody List<Integer> goodsIds){
        return goodsService.batchDel(goodsIds);
    }


    /**
     * 图片上传(OSS)
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file")MultipartFile file){
        String filePath = OssUtil.uploadMultipartFile(file);
        return Result.success(filePath);
    }
}
